import ajax from 'uni-ajax'
import baseConfig from './config.js'
import store from 'store/index.js'
import {
	getToken
} from './auth.js'

const request = ajax.create({
	baseURL: baseConfig.baseURL
})

// 请求拦截
request.interceptors.request.use(config => {
	let toKen = getToken()
	if (toKen) {
		// if (config.toKenAfter) {
			config.header['Authorization'] = `${uni.$config.toKenAfter} ${toKen}`
		// } else {
		// 	config.header[baseConfig.toKenKey] = toKen
		// }
	}
	config.header['Content-Type'] = 'application/json'
	return config
}, err => {
	return Promise.reject(err)
})

// 响应拦截
request.interceptors.response.use(res => {
	return res.data
}, err => {
	// 	if (err.data.message == '当前用户登录已过期') {
	// 		uni.clearStorageSync();
	// 		uni.reLaunch({
	// 			url: '/pages/index/index'
	// 		});
	// 	}

	// 	return Promise.reject(err)
})
export default request